package com.qianyu.project.web.controller.lyz;

import com.qianyu.project.entity.MaterialContract;
import com.qianyu.project.entity.MaterialContractDetail;
import com.qianyu.project.service.IMaterialContractService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lyz
 * @date create in 2021/2/9 16:08
 */
@RestController
@RequestMapping("/MaterialContract")
public class MaterialContractController {
    @Autowired
    private IMaterialContractService materialContractService;

    @GetMapping("")
    public Result getMaterialContractList(@RequestParam Map<String,String> map){
        System.out.println(map);
        return ResultTool.success(materialContractService.list());
    }

    @PostMapping("")
    public Result saveMaterialContract(@RequestBody Map<String,Object> map){
        System.out.println(map);
        MaterialContract contract=new MaterialContract();
        Map<String,Object> map1= (Map<String, Object>) map.get("contract");
        contract.setPId(Integer.parseInt(map1.get("pId").toString()));
        contract.setContractName(map1.get("contractName").toString());
        contract.setContractType(Integer.parseInt(map1.get("contractType").toString()));
        contract.setProviderId(Integer.parseInt(map1.get("providerId").toString()));
        contract.setContractSignatory(map1.get("contractSignatory").toString());
        contract.setSettleType(map1.get("settleType").toString());
        contract.setStartDate(map1.get("startDate").toString());
        contract.setEndDate(map1.get("endDate").toString());
        contract.setProjectPeriod(map1.get("projectPeriod").toString());
        contract.setContractAmount(new BigDecimal(map1.get("contractAmount").toString()));

        contract.setPaymentType(map1.get("paymentType").toString());
        contract.setAdvancePay(new BigDecimal(map1.get("advancePay").toString()));
        contract.setDepositAmount(new BigDecimal(map1.get("depositAmount").toString()));
        contract.setPaymentCondition(map1.get("paymentCondition").toString());
        contract.setMainTerm(map1.get("mainTerm").toString());
        contract.setComment(map1.get("comment").toString());
        contract.setFlowId(1);
        contract.setApproveStatus("1");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        contract.setCreateDate(sdf.format(new Date()));

        List<Map<String,Object>> list= (List<Map<String, Object>>) map.get("details");
        List<MaterialContractDetail> detailList=new ArrayList<MaterialContractDetail>();
        list.forEach(d->{
            MaterialContractDetail detail = new MaterialContractDetail();
            detail.setMateId(Integer.parseInt(d.get("mateTypeId").toString()));
            detail.setBuyCount(new BigDecimal(d.get("buyCount").toString()));
            detail.setBuyPrice(new BigDecimal(d.get("buyPrice").toString()));
            detail.setTaxPercent(new BigDecimal(d.get("taxPercent").toString()).divide(new BigDecimal(100)));
            detailList.add(detail);
        });
        System.out.println(contract);
        System.out.println(detailList);

        materialContractService.saveMaterialContract(contract,detailList);
        return ResultTool.success();
    }
}